"""
PROJECT: django_celery_demo
FILE: search_indexes.py
AUTHOR: wensen
CREATE: 2022-03-04 
"""
from haystack import indexes

from haystack_demo.models import Goods


class GoodsIndex(indexes.SearchIndex, indexes.Indexable):
    # 类名必须为需要检索的Model_name+Index，这里需要检索Goods，所以创建GoodsIndex
    text = indexes.CharField(document=True, use_template=True)  # 创建字段名必须为text
    # 其它字段
    goods_name = indexes.CharField(model_attr='goods_name')
    goods_description = indexes.CharField(model_attr='goods_description')

    create_time = indexes.DateTimeField(model_attr='create_time')
    price = indexes.DecimalField(model_attr="price")
    inventory = indexes.IntegerField(model_attr="inventory")
    goods_id = indexes.IntegerField(model_attr="id")

    def get_model(self):  # 重载get_model方法，必须要有！
        return Goods

    def index_queryset(self, using=None):
        return self.get_model().objects.all()
